﻿var searchdata = { Text: "请选择", Value: "" }
function addsearch(combobox) {
    combobox.html("");
    combobox.append(getOption(searchdata.Value, searchdata.Text));
}
function getOption(value, text) {
    return "<option value=" + value + ">" + text + "</option>"
}
function setArea(provinceid, cityid, countryid) {

    var sel_province = $(provinceid);
    var sel_city = $(cityid);
    var sel_country = $(countryid);


    addsearch(sel_province);
    addsearch(sel_city);
    addsearch(sel_country);
    $(data_province).each(function() {

        sel_province.append(getOption(this.Value, this.Text));

    });
    sel_province.change(function() {
        var selectedindex = this.selectedIndex;
        if (selectedindex == 0) {
            addsearch(sel_province);
            addsearch(sel_city);
            addsearch(sel_country);
        }
        else {
            var selectValue = this.options[selectedindex].value;
            $(data_city).each(function() {
                if (this.Parentvalue == selectValue) {
                    addsearch(sel_city);
                    $(this.Data).each(function() {
                        sel_city.append(getOption(this.Value, this.Text));
                    });
                }
            });
        }
    });
    sel_city.change(function() {
        var selectedindex = this.selectedIndex;
        if (selectedindex == 0) {
            addsearch(sel_country);
        }
        else {
            var selectValue = this.options[selectedindex].value;
            $(data_country).each(function() {
                if (this.Parentvalue == selectValue) {
                    addsearch(sel_country);
                    $(this.Data).each(function() {
                        sel_country.append(getOption(this.Value, this.Text));
                    });
                }
            });
        }
    });
}




function setArea(provinceid, cityid, countryid, provincevlaueid, cityvalueid, countryvalueid) {

    var sel_province = $(provinceid);
    var sel_city = $(cityid);
    var sel_country = $(countryid);


    var province_value = $(provincevlaueid);
    var city_value = $(cityvalueid);
    var country_value = $(countryvalueid);


    var province_name = $(provincevlaueid+"_name");
    var city_name = $(cityvalueid + "_name");
    var country_name = $(countryvalueid + "_name");
    



    addsearch(sel_province);
    addsearch(sel_city);
    addsearch(sel_country);
    $(data_province).each(function() {

        sel_province.append(getOption(this.Value, this.Text));

    });
    sel_province.change(function() {
        var selectedindex = this.selectedIndex;
        var selectValue = this.options[selectedindex].value;
        var selectText = this.options[selectedindex].text;
        province_value.attr("value", selectValue);
        if (selectedindex > 0) {
            province_name.attr("value", selectText);
        }    
        if (selectedindex == 0) {
            addsearch(sel_province);
            addsearch(sel_city);
            addsearch(sel_country);
            province_name.attr("value", "");
            city_name.attr("value", "");
            country_name.attr("value", "");
        }
        else {
            $(data_city).each(function() {
                if (this.Parentvalue == selectValue) {
                    addsearch(sel_city);
                    $(this.Data).each(function() {
                        sel_city.append(getOption(this.Value, this.Text));
                    });
                }
            });
        }
    });
    sel_city.change(function() {
        var selectedindex = this.selectedIndex;
        var selectValue = this.options[selectedindex].value;
        var selectText = this.options[selectedindex].text;
        city_value.attr("value", selectValue);
        if (selectedindex > 0) {
            city_name.attr("value", selectText);
        }
        if (selectedindex == 0) {
            addsearch(sel_country);
            city_name.attr("value", "");
            country_name.attr("value", "");
        }
        else {
            $(data_country).each(function() {
                if (this.Parentvalue == selectValue) {
                    addsearch(sel_country);
                    $(this.Data).each(function() {
                        sel_country.append(getOption(this.Value, this.Text));
                    });
                }
            });
        }
    });

    sel_country.change(function() {

        var selectedindex = this.selectedIndex;
        var selectValue = this.options[selectedindex].value;
        var selectText = this.options[selectedindex].text;
        country_value.attr("value", selectValue);
        if (selectedindex > 0) {
            country_name.attr("value", selectText);
        }
        else {
            country_name.attr("value", "");
        }

    });
    
    
}